﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MyToDoList.Model;
using MyToDoList.ViewModel;
using Microsoft.Phone.Shell;

using System.Collections.ObjectModel;
using System.ComponentModel;

namespace MyToDoList
{
    public partial class MainPage : PhoneApplicationPage
    {
        ObservableCollection<Task> toDoTasks = new ObservableCollection<Task>();
        ObservableCollection<Task> doneTasks = new ObservableCollection<Task>();
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            if(App.ViewModel.IsDataLoaded)
                App.ViewModel.LoadData();

                toDoTasks = new ObservableCollection<Task>(App.ViewModel.Items.Where(i => !i.Done).ToList<Task>());

                doneTasks = new ObservableCollection<Task>(App.ViewModel.Items.Where(i => i.Done).ToList<Task>());

                lsToDoTask.ItemsSource = toDoTasks;

                lsToDoTask.DataContext = App.ViewModel;

                lsDoneTask.ItemsSource = doneTasks;

                lsDoneTask.DataContext = App.ViewModel;
        }

        private void EnableDisableFunctionButtons(bool isEnabled)
        {
            
            //((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = !isEnabled;
        }


        private void taskPanorama_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            switch (taskPanorama.SelectedIndex)
            {
                case 0:
                    {
                        ((ApplicationBarIconButton)ApplicationBar.Buttons[3]).IsEnabled = true;
                        deselectItems();
                    }
                    break;

                case 1:
                    {
                        ((ApplicationBarIconButton)ApplicationBar.Buttons[3]).IsEnabled = false;
                        deselectItems();
                    }
                    break;
            }

        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/MyToDoList;component/UI/DetailElement.xaml", UriKind.RelativeOrAbsolute));
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            var res = toDoTasks.Where(t => t.IsChecked);
            
            if (res.Count() == 0)
                return;
            if (res.Count() > 1)
            {
                MessageBox.Show("Select only one task", "Warning", MessageBoxButton.OK);
                return;
            }
            App.CurrentObject = res.First();
            this.NavigationService.Navigate(new Uri("/MyToDoList;component/UI/DetailElement.xaml", UriKind.RelativeOrAbsolute));
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            foreach (Task task in toDoTasks)
            {
                if (!task.IsChecked)
                    continue;
                task.IsChecked = false;
                App.ViewModel.Items.Remove(task);
                App.ViewModel.IsDataLoaded = false;
            }
            foreach (Task task in doneTasks)
            {
                if (!task.IsChecked)
                    continue;
                task.IsChecked = false;
                App.ViewModel.Items.Remove(task);
                App.ViewModel.IsDataLoaded = false;
            }
            LoadData();
        }

        private void btDone_Click(object sender, EventArgs e)
        {
            foreach (Task task in toDoTasks)
            {
                if (task.IsChecked)
                {
                    task.Done = true;
                    task.IsChecked = false;
                    App.ViewModel.Update(task);
                    App.ViewModel.IsDataLoaded = false;
                }
            }
            LoadData();

        }

        private void deselectItems()
        {
            if (App.ViewModel.IsDataLoaded)
            {
                foreach (var item in lsToDoTask.Items)
                {
                    Task t = item as Task;
                    if (t.IsChecked)
                    {
                        t.IsChecked = false;
                        App.ViewModel.Update(t);
                    }
                }
                foreach (var item in lsDoneTask.Items)
                {
                    Task t = item as Task;
                    if (t.IsChecked)
                    {
                        t.IsChecked = false;
                        App.ViewModel.Update(t);
                    }
                }
                LoadData();
            }
        }

    }
}